import {set} from "nprogress";
import {Graph} from "@antv/x6";

export interface IResizeCompositionArgs {
  graph: Graph,
  containerId: string,
}

export function useWorktableResize({graph, containerId}: IResizeCompositionArgs) {
  let resizeDelayTimer: ReturnType<typeof setTimeout>

  const resize = () => {
    clearTimeout(resizeDelayTimer);
    resizeDelayTimer = setTimeout(() => {
      const containerWrapper = document.querySelector(containerId)

      if (containerWrapper) {
        const containerWrapperRect = containerWrapper.getBoundingClientRect();
        graph.resize(containerWrapperRect.width, containerWrapperRect.height);
      }
    }, 1000);
  }

  const bindResizeEvent = () => {
    window.addEventListener("resize", resize);
  }

  const unbindResizeEvent = () => {
    window.removeEventListener("resize", resize)
  }

  onMounted(() => {
    bindResizeEvent()
  })

  onBeforeUnmount(() => {
    unbindResizeEvent()
  })
}
